Imports Microsoft.VisualBasic

Public Class WebPageBase
    Inherits System.Web.UI.Page

#Region " Enums, Memebers and Properties "

    Protected Enum UserControls

        demo

        'Admin Folder Items
        adm_Themes
        adm_Nodes
        adm_Roles
        adm_Site
        adm_Unlock
        adm_UserDetails
        adm_Users
        adm_Boards
        adm_Categories
        adm_Forums
        adm_Topics
        adm_Security

        'Control Panel Folder Item
        cp_avatar
        cp_MemberList
        cp_Profile
        cp_ProfileView
        cp_UserActivity

        'Blog Folder Item
        blg_addblog
        blg_blogviewer
        blg_myblogs
        blg_BlogList

        'Chat Folder Item
        cht_chatroom

        'Event
        eve_AddEvent
        eve_Events
        eve_EventViewer
        eve_MyEvents

        'Form Folder Item
        form_form
        form_formanalysis
        form_formlist
        form_formmanager        
        form_formresults
        form_formviewer

        'Forum Folder Item
        for_Board
        for_Category
        for_Forum
        for_Post
        for_Topic
        for_AddPost
        for_AddTopic
		for_PendingPosts
		for_RecentActivity

        'Gallery
        gal_Galleries
        gal_GalleryViewer
        gal_MyGallery

        'Messaging Folder Items
        msg_Email
        msg_Inbox
        msg_Message
        msg_PrivateMessage
        msg_SentItems
        msg_MessageSystem

        'News Folder Items
        news_addnews
		news_newslist
		news_newsarchive

        'Options Folder Items
        opt_addcomment
        opt_comments
        opt_menubuilder
        opt_attachments

        'Polls Folder Items
        pol_addpoll
        pol_polllist
        pol_pollviewer

        'Reports Folder Items
        rpt_TopNByDate
        rpt_Revisions

        'Resources Folder Items
        res_Download
        res_Resourcegallery
        res_Thumbnail
        res_Upload
        res_Flash
        res_getresource
        Download
        Thumbnail

        'Security Folder Item
        sec_ChangePassword
        sec_GetPassword
        sec_Login
        sec_NewUser
		sec_Banned
		sec_Verification

        'Template Folder Items
        tpl_Template
        tpl_TemplateList
        tpl_TemplateView

        'UserControls Folder Items
        Main
        Editor
        [Error]
        List
        Management
        News
        Pagelinks
        Redirect
        Results
        Search
        Viewer
        Weblinks
        Comments
    End Enum

    Protected Enum PageList
        DefaultPage = 0
        ForumPage = 1
        BlogPage = 2
        EventPage = 3
        GalleryPage = 4
        PollPage = 5
    End Enum

#End Region

#Region " Cookie Methods "

    Protected Function GetGuidFromQueryCookie(ByVal ItemValue As String) As Guid

        If IsNothing(Request.Cookies(ItemValue)) Then
            Return Guid.Empty
        Else
            Return New Guid(Request.Cookies(ItemValue).ToString)
        End If

    End Function

    Protected Function GetStringFromQueryCookie(ByVal ItemValue As String) As String

        If IsNothing(Request.Cookies(ItemValue)) Then
            Return String.Empty
        Else
            Return New String(Request.Cookies(ItemValue).ToString)
        End If

    End Function

    Protected Function GetIntegerFromQueryCookie(ByVal ItemValue As String) As Integer

        If IsNothing(Request.Cookies(ItemValue)) Then
            Return 0
        Else
            Return CType(Request.Cookies(ItemValue).ToString, Integer)
        End If

    End Function

    Protected Function GetBooleanFromQueryCookie(ByVal ItemValue As String) As Boolean

        If IsNothing(Request.QueryString(ItemValue)) Then
            Return False
        Else
            Return CType(Request.Cookies(ItemValue).ToString, Boolean)
        End If

    End Function

#End Region

#Region " QueryString Methods "

    Public Function GetGuidFromQueryString(ByVal ItemValue As String) As Guid

        If IsNothing(Request.QueryString(ItemValue)) Then
            Return Guid.Empty
        Else
            Return New Guid(Request.QueryString(ItemValue))
        End If

    End Function

    Public Function GetStringFromQueryString(ByVal ItemValue As String) As String

        If IsNothing(Request.QueryString(ItemValue)) Then
            Return String.Empty
        Else
            Return New String(Request.QueryString(ItemValue))
        End If

    End Function

    Public Function GetIntegerFromQueryString(ByVal ItemValue As String) As Integer

        If IsNothing(Request.QueryString(ItemValue)) Then
            Return 0
        Else
            Return CType(Request.QueryString(ItemValue), Integer)
        End If

    End Function

    Public Function GetBooleanFromQueryString(ByVal ItemValue As String) As Boolean

        If IsNothing(Request.QueryString(ItemValue)) Then
            Return False
        Else
            Return CType(Request.QueryString(ItemValue), Boolean)
        End If

    End Function

#End Region

#Region " Session Methods "

    Protected Function GetGuidFromSession(ByVal ItemValue As String) As Guid

        If IsNothing(Session(ItemValue)) Then
            Return Guid.Empty
        Else
            Return New Guid(Session(ItemValue).ToString)
        End If

    End Function

    Protected Function GetStringFromSession(ByVal ItemValue As String) As String

        If IsNothing(Session(ItemValue)) Then
            Return String.Empty
        Else
            Return New String(Session(ItemValue))
        End If

    End Function

    Protected Function GetIntegerFromSession(ByVal ItemValue As String) As Integer

        If IsNothing(Session(ItemValue)) Then
            Return 0
        Else
            Return CType(Session(ItemValue), Integer)
        End If

    End Function

    Protected Function GetBooleanFromSession(ByVal ItemValue As String) As Boolean

        If IsNothing(Session(ItemValue)) Then
            Return False
        Else
            Return CType(Session(ItemValue), Boolean)
        End If

    End Function

#End Region

#Region " Methods "

    Protected Function IsThemeValid(ByVal theme As String) As Boolean
        Return System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("~/App_Themes/" & theme))
    End Function

    Protected Function FindControlItem(ByVal PageType As PageList) As String
        Dim mainPath As String
        Dim FormBase As UserControlBase = New UserControlBase
        Dim actualPage As String = String.Empty
        Dim ControlItem As String

        mainPath = WebBase.AppPath

        Try
            If Request.QueryString("pid").IndexOf("res_flash") = 0 Then
                actualPage = "flash"
            Else
                actualPage = CType(System.Enum.Parse(GetType(UserControls), Request.QueryString("pid"), True), UserControls).ToString
            End If
        Catch generatedExceptionVariable0 As Exception

            If PageType = PageList.DefaultPage Then
                actualPage = UserControls.Main.ToString
            End If

            If PageType = PageList.ForumPage Then
                actualPage = UserControls.for_Board.ToString
            End If

            If PageType = PageList.BlogPage Then
                actualPage = UserControls.blg_BlogList.ToString
            End If

            If PageType = PageList.GalleryPage Then
                actualPage = UserControls.gal_Galleries.ToString
            End If

            If PageType = PageList.EventPage Then
                actualPage = UserControls.eve_Events.ToString
            End If

            If PageType = PageList.PollPage Then
                actualPage = UserControls.pol_polllist.ToString
            End If

        Finally
            ControlItem = String.Format("{0}Usercontrols/{1}.ascx", mainPath, actualPage)
        End Try

        If ControlItem.IndexOf("/adm_") >= 0 Then
            ControlItem = ControlItem.Replace("/adm_", "/Admin/")
        End If

        If ControlItem.IndexOf("/blg_") >= 0 Then
            ControlItem = ControlItem.Replace("/blg_", "/Blog/")
        End If

        If ControlItem.IndexOf("/cht_") >= 0 Then
            ControlItem = ControlItem.Replace("/cht_", "/Chat/")
        End If

        If ControlItem.IndexOf("/cp_") >= 0 Then
            ControlItem = ControlItem.Replace("/cp_", "/ControlPanel/")
        End If

        If ControlItem.IndexOf("/eve_") >= 0 Then
            ControlItem = ControlItem.Replace("/eve_", "/Event/")
        End If

        If ControlItem.IndexOf("/form_") >= 0 Then
            ControlItem = ControlItem.Replace("/form_", "/Form/")
        End If

        If ControlItem.IndexOf("/for_") >= 0 Then
            ControlItem = ControlItem.Replace("/for_", "/Forum/")
        End If

        If ControlItem.IndexOf("/gal_") >= 0 Then
            ControlItem = ControlItem.Replace("/gal_", "/Gallery/")
        End If

        If ControlItem.IndexOf("/hlp_") >= 0 Then
            ControlItem = ControlItem.Replace("/hlp_", "/Help/")
        End If

        If ControlItem.IndexOf("/msg_") >= 0 Then
            ControlItem = ControlItem.Replace("/msg_", "/Messaging/")
        End If


        If ControlItem.IndexOf("/news_") >= 0 Then
            ControlItem = ControlItem.Replace("/news_", "/News/")
        End If

        If ControlItem.IndexOf("/opt_") >= 0 Then
            ControlItem = ControlItem.Replace("/opt_", "/Options/")
        End If

        If ControlItem.IndexOf("/pol_") >= 0 Then
            ControlItem = ControlItem.Replace("/pol_", "/Polls/")
        End If

        If ControlItem.IndexOf("/rpt_") >= 0 Then
            ControlItem = ControlItem.Replace("/rpt_", "/Reports/")
        End If

        If ControlItem.IndexOf("/sec_") >= 0 Then
            ControlItem = ControlItem.Replace("/sec_", "/Security/")
        End If

        'Specific for playing flash files
        If ControlItem.IndexOf("/flash") >= 0 Then
            ControlItem = ControlItem.Replace("/flash", "/Resources/Flash")
        End If

        If ControlItem.IndexOf("/res_") >= 0 Then
            ControlItem = ControlItem.Replace("/res_", "/Resources/")
        ElseIf ControlItem.IndexOf("/Download") >= 0 Then
            ControlItem = ControlItem.Replace("/Download", "/Resources/Download")
        ElseIf ControlItem.IndexOf("/Thumbnail") >= 0 Then
            ControlItem = ControlItem.Replace("/Thumbnail", "/Resources/Thumbnail")
        End If

        If ControlItem.IndexOf("/tpl_") >= 0 Then
            ControlItem = ControlItem.Replace("/tpl_", "/Template/")
        End If

        Return ControlItem

    End Function

#End Region

#Region " Page Events "

    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit

        Dim config As New WebConfigSettings
        Dim PU As New ProfileUtility

        If GetBooleanFromQueryString("printme") = True Then
            Page.Theme = "PrintMe"
            Page.MasterPageFile = config.PopUpMasterPage
        Else
            Try

                If HttpContext.Current.User.Identity.IsAuthenticated Then

                    Dim User As String = HttpContext.Current.User.Identity.Name
                    Dim Theme As String = PU.GetTheme(User)

                    If Not Theme = String.Empty Then
                        If IsThemeValid(Theme) Then
                            Page.Theme = Theme
                        Else
                            Page.Theme = WebBase.AppTheme
                        End If
                    Else
                        Page.Theme = WebBase.AppTheme
                    End If
                Else
                    Page.Theme = WebBase.AppTheme
                End If

                If GetBooleanFromQueryString("popup") = True Then
                    Page.MasterPageFile = config.PopUpMasterPage
                Else
                    Page.MasterPageFile = config.MasterPage
                End If
            Catch ex As Exception
                Page.Theme = WebBase.AppTheme
            End Try
        End If

    End Sub

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If WebBase.AppInstalled = False Then
            Response.Redirect("install/dbinstall.aspx")
        End If

    End Sub

    Public Overloads Sub RegisterOnSubmitStatement(ByVal key As String, ByVal script As String)
        ScriptManager.RegisterOnSubmitStatement(Me, GetType(Page), key, script)
    End Sub

#End Region

End Class
